package com.drg.center.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.drg.center.entity.DictDiagnosisHisPO;
import com.drg.center.mapper.DictDiagnosisHisMapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DictDiagnosisHisDAO extends BaseDAO<DictDiagnosisHisMapper, DictDiagnosisHisPO> {

    public List<DictDiagnosisHisPO> searchDiagnosis(String search) {
        LambdaQueryWrapper<DictDiagnosisHisPO> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(search)){
            queryWrapper.like(DictDiagnosisHisPO::getDiagnosisCode, search)
                    .or().like(DictDiagnosisHisPO::getDiagnosisName, search)
                    .or().like(DictDiagnosisHisPO::getHisDiagnosisName, search)
                    .or().like(DictDiagnosisHisPO::getHisDiagnosisCode, search);
        }
        queryWrapper.last("limit 0,20");
        return getBaseMapper().selectList(queryWrapper);
    }

    public List<DictDiagnosisHisPO> searchHospitalDiagnosis(String search) {
        LambdaQueryWrapper<DictDiagnosisHisPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(DictDiagnosisHisPO::getDiagnosisCode);
        if(StringUtils.isNotBlank(search)){
            queryWrapper.and(wrapper -> wrapper.like(DictDiagnosisHisPO::getHisDiagnosisCode, search)
                    .or().like(DictDiagnosisHisPO::getHisDiagnosisName, search));
        }
        queryWrapper.last("limit 0,20");
        return getBaseMapper().selectList(queryWrapper);
    }

    public List<DictDiagnosisHisPO> searchMatchDiagnosis() {
        LambdaQueryWrapper<DictDiagnosisHisPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(DictDiagnosisHisPO::getDiagnosisCode);
        return getBaseMapper().selectList(queryWrapper);
    }

    public List<DictDiagnosisHisPO> searchMatchDiagnosis(String search) {
        LambdaQueryWrapper<DictDiagnosisHisPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(DictDiagnosisHisPO::getDiagnosisCode).isNotNull(DictDiagnosisHisPO::getHisDiagnosisCode);
        if(StringUtils.isNotBlank(search)){
            queryWrapper.and(wrapper -> wrapper.like(DictDiagnosisHisPO::getDiagnosisCode, search)
                    .or().like(DictDiagnosisHisPO::getDiagnosisName, search)
                    .or().like(DictDiagnosisHisPO::getHisDiagnosisName, search)
                    .or().like(DictDiagnosisHisPO::getHisDiagnosisCode, search));
            queryWrapper.last("limit 0,20");
        }
        return getBaseMapper().selectList(queryWrapper);
    }

    public int cancelMatchDiagnosis(List<Long> ids) {
        LambdaUpdateWrapper<DictDiagnosisHisPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(DictDiagnosisHisPO::getDiagnosisCode, null);
        updateWrapper.set(DictDiagnosisHisPO::getDiagnosisName, null);
        updateWrapper.in(DictDiagnosisHisPO::getId, ids);
        return getBaseMapper().update(new DictDiagnosisHisPO(), updateWrapper);
    }
}
